package pl.gigiel.behaviour.environment.factory;

import java.lang.reflect.Constructor;

import pl.gigiel.behaviour.IEnvironment;
import pl.gigiel.behaviour.IEnvironmentFactory;
import pl.gigiel.behaviour.exception.BehaviourException;


public class EnvironmentFactory implements IEnvironmentFactory {
	private static IEnvironmentFactory environmentFactory;

	private EnvironmentFactory() {
	}

	public static IEnvironmentFactory getInstance() {
		if (environmentFactory == null) {
			environmentFactory = new EnvironmentFactory();
		}
		return environmentFactory;
	}

	@Override
	public <T extends IEnvironment> IEnvironment createEnvironment(Class<T> environmentClass) throws BehaviourException {
		IEnvironment environment = null;
		try {
			Constructor<T> constructor = environmentClass.getConstructor();
			environment = constructor.newInstance();
		} catch (Exception e) {
			throw new BehaviourException(e);
		}
		environment.init();
		return environment;
	}
}
